<% layout('layout/layout') %>
<% block('header').append(`<style>
</style>`) %>
<div class="row">
    <div class="col-md-12">
        <div class="box box-info">
            <div class="box-header with-border">
                <h3 class="box-title">订单号：<%=order.orderId %><span style="padding-left:20px;font-size:18px;"> 下单时间：<%=order.create_at_ago()%></span></h3>
            </div>
            <!-- /.box-header -->
            <div class="box-body">
                <p>收件人：<%=order.address.name %></p>
                <p>联系方式：<%=order.address.mobile %></p>
                <p>收获地址：<%=order.address.place %></p>
            </div>
            <!-- /.box-body -->
            <!--<div class="box-footer">-->

            <!--</div>-->
                <!-- /.box-footer -->
        </div>
        <div class="box box-info">
            <div class="box-header with-border">
                <h3 class="box-title">
                    订单状态： <span class="padding-left20 <%=order.statusColor %>"><%=order.statusName%></span>
                    <% if(order.postageId) { %>
                    <span class="text-muted fs12">[关联的物流编号：<%=order.postageId %>]</span>
                    <% } %>
                </h3>
            </div>
            <!-- /.box-header -->
            <div class="box-body">
                <div class="col-md-6" style="border-right:1px solid #e3e3e3;">
                    <div >
                        <p><span class="text-muted text-label">商品合计：</span><span class="text-label">￥<%=order.totalPrice%></span></p>
                        <p><span class="text-muted text-label">运费：</span><span class="text-label">￥<%=order.feePrice %></span></p>
                    </div>
                    <div class="padding-top" style="border-top:1px dashed #e3e3e3;">
                        <p>
                            <span class="text-muted text-label">应付：</span><span class="text-label text-danger fs20">￥<%=order.needPrice %></span>
                            <% if(order.offerType){ %>
                            <span class="text-info fs12 padding-left">[已改价优惠：￥<%=order.offerPrice%>]</span>
                            <% }%>
                        </p>
                    </div>
                </div>
                <div class="col-md-6">
                    <div >
                        <p>相关操作</p>
                        <% if(order.orderStatus === 10){ %>
                        <a id="cancelOrder" class="btn btn-default btn-sm" data-url="/admin/order/cancel/<%=order._id %>" href="javascript:void(0)" >取消订单</a>
                        <a id="changeAmt"  class="btn btn-primary btn-sm" data-needprice="<%=order.needPrice %>" data-url="/admin/order/changeAmt/<%=order._id %>" href="javascript:void(0)" >改价</a>
                        <% } %>
                        <% if(order.orderStatus >=20 && order.orderStatus < 30){ %>
                            <a id="linkTrain" class="btn btn-info btn-sm" data-url="/admin/order/linkTrain/<%=order._id %>"   href="javascript:void(0)" >关联物流信息</a>
                            <a id="changeOrder" class="btn btn-success btn-sm" data-url="/admin/order/linkTrain/<%=order._id %>" href="javascript:void(0)" >无物流直接修改已发货</a>
                            <a id="backAmt" data-price="<%=order.needPrice%>" class="btn btn-danger btn-sm" data-url="/admin/order/backAmt/<%=order._id %>"  href="javascript:void(0)" >退款</a>
                        <% } %>
                        <% if(order.orderStatus >= 30 && order.orderStatus < 40) { %>
                            <% if (order.postageId) { %>
                              <a id="queryPostage" class="btn btn-success btn-sm"  data-id="<%=order.postageId %>"   href="javascript:void(0)" >查看物流详情</a>
                            <% } %>
                            <a id="finishBtn" class="btn btn-success btn-sm" data-url="/admin/order/finish/<%=order._id %>"  href="javascript:void(0)" >设置订单完成</a>
                            <a id="backAmt" data-price="<%=order.needPrice%>" class="btn btn-danger btn-sm" data-url="/admin/order/backAmt/<%=order._id %>"  href="javascript:void(0)" >退款</a>
                        <% }%>
                          <% if(order.orderStatus === 40) { %>
                            <a id="backAmt" data-price="<%=order.needPrice%>" class="btn btn-danger btn-sm" data-url="/admin/order/backAmt/<%=order._id %>"  href="javascript:void(0)" >退款</a>
                          <% }%>
                        <!--<a id="queryOrderPay" class="btn btn-info btn-sm" data-id="<%=order.id %>" data-orderid="<%=order.orderId %>" data-url="/order/linkTrain/<%=order._id %>"   href="javascript:void(0)" >查询订单支付状况</a>-->
                    </div>
                </div>
            </div>
            <!-- /.box-body -->
            <!--<div class="box-footer">-->

            <!--</div>-->
            <!-- /.box-footer -->
        </div>


        <div class="box box-info">
            <div class="box-header with-border">
                <h3 class="box-title">商品</h3>
            </div>
            <!-- /.box-header -->
            <div class="box-body">
                <table class="order_goods_table table text-center" style="width:100%">
                    <thead>
                    <tr class="text-center">
                        <th style="width:280px;" >商品信息</th>
                        <th class="text-center">单价</th>
                        <th class="text-center">数量</th>
                        <th class="text-center">小计</th>
                        <th class="text-center">实付</th>
                    </tr>
                    </thead>
                    <tbody>
                    <% order.goods.forEach(item => { %>
                    <tr>
                        <td class="text-left" style="text-align:left;padding-left:0;">
                            <div class="vertical-top" style="vertical-align:top;display:inline-block;width:80px;border:1px solid #e3e3e3;height:80px;background:url(<%=item.imgTmb %>) no-repeat center;background-size:cover;"></div>
                            <span class="padding-top5"><%=item.name %></span>
                        </td>
                        <td>￥<%=item.sellPrice %></td>
                        <td><%= item.num %></td>
                        <td>￥<%= formatFloat(item.sellPrice * item.num) %></td>
                        <td>￥<%= formatFloat(item.sellPrice * item.num) %></td>
                    </tr>
                    <% }) %>
                    </tbody>
                </table>
            </div>
            <!-- /.box-body -->
            <!-- /.box-footer -->
        </div>
    </div>
</div>
<% block('footer').append(`<script id="queryPostageTpl" type="text/template">
<div>
  <ul class="traces_ul">
  <@ Traces.forEach(function (item) { @>
  <li><@=item.AcceptStation @></li>
  <@ }) @>
</ul>
</div>
</script>`) %>
<% block('footer').append(`<script>
  jQuery(function() {
  	var tplComplie = _.template($('#queryPostageTpl')[0].innerHTML)
  	var needPrice = '${order.needPrice}'
    $('#cancelOrder').on('click',function() {
      var $this = $(this)
      $.confirm('你确定要帮助用户取消这笔订单么?','取消订单').then(function(res){
      	console.log(res)
      	$.post($this.data('url')).done(function(res){
      		location.reload()
      	})
      })
    })
    $('#changeAmt').on('click', function () {
      var $this = $(this)
      $.prompt('你填写修改的订单支付总金额,当前应付金额为<span class="text-danger"> ￥' + needPrice +'</span>','改价', {
        regx:/(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/,
        errorMsg: '请输入有效金额',
        placeholder: '输入要修改的金额',
      }).then(function(res){
        $.post($this.data('url'),{amt: $.formatFloat(res)}).done(function(res){
          location.reload()
        })
      	//console.log(res)
      })
    })
    $('#backAmt').on('click', function () {
       var $this = $(this)
       $.confirm('你确定操作退款么，已支付金额将会通过微信返还用户?','退款').then(function(res){
      	 $.post($this.data('url'), {amt: $this.data('price')}).done(function(res){
            $.alert('<p>退款请求已提交!</p><p>退款结果可能会有延时</p><p>请告知用户1～3个工作日内到帐</p>').then(function(){
              location.reload()
            })
      	 })
      })
    })
    $('#linkTrain').on('click', function (e) {
    	var $this = $(this)
      $.prompt('请输入你要关联的物流单号','关联物流单号',{placeholder:'请输入订单的物流编号'}).then(function(res){
      	 $.post($this.data('url'),{postId:res}).done(function(res){
            $.alert('<p>物流单号关联成功</p>').then(function(){
              location.reload()
            })
      	 })
      })
    })
    $('#changeOrder').on('click', function () {
      var $this = $(this)
      $.confirm('你确定不关联物流单号直接修改订单状态么？','修改订单状态').then(function(res){
         $.post($this.data('url'),{postId:''}).done(function(res){
            $.alert('修改订单状态为已发货成功').then(function(){
              location.reload()
            })
      	 })
      })
    })
    $('#finishBtn').on('click', function () {
      var $this = $(this)
      $.confirm('你确定修改订单为已完成么？','提示').then(function(res){
         $.post($this.data('url')).done(function(res){
            $.alert('成功修改订单状态').then(function(){
              location.reload()
            })
      	 })
      })
    })

  $('#queryPostage').on('click',function () {
    var $this = $(this)
    $this.prop('disabled',true).text('正在查询...')
    $.post('/queryPostage/' + $this.data('id')).done(res=> {
      console.log(res)
      $this.prop('disabled',false).text('查询物流详情')
        BootstrapDialog.show({
          title:"物流详情",
          message: $(tplComplie(res.data.postage)),
          buttons: [{
            label: '确定',
            action: function(dialog) {
              dialog.close();
            }
          }]
        });
    }).fail(err => {
    	$this.prop('disabled',false).text('查询物流详情')
      console.log(err)
    })
  })
//    $('#queryOrderPay').on('click', function () {
//    	var $this = $(this)
//    	$.post('/order/isPay',{id: $this.data('id'),orderId: $this.data('orderid')}).done(function(res){
//        console.log(res)
//    	})
//    })
  })
</script>`) %>